﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteBase;
namespace LTO
{
    /// <summary>
    /// Interaction logic for MessageServer.xaml
    /// </summary>
    public partial class MessageServer : Window
    {
        public MessageServer()
        {
            InitializeComponent();
            startService();
        }
        TcpChannel channel;
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            startService();
        }

        private void startService()
        {
            if (channel == null)
            {
                channel = new TcpChannel(8080);
                ChannelServices.RegisterChannel(channel, false);
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(SampleObject), "ChatRoom", WellKnownObjectMode.Singleton);
                string Title = ((System.Runtime.Remoting.Channels.ChannelDataStore)(channel.ChannelData)).ChannelUris[0];
                lblStatus.Content = string.Format("Message View is Runing at {0}", Title);
                btnStart.IsEnabled = false;
                btnStop.IsEnabled = true;
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            stopSerice();
        }

        private void stopSerice()
        {
            if (channel != null)
            {
                ChannelServices.UnregisterChannel(channel);
                channel = null;
                lblStatus.Content = "Message View is Stop.";
                btnStart.IsEnabled = true;
                btnStop.IsEnabled = false;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            stopSerice();
        }
    }
}
